package fr.oz.mobile.common.horloge 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.sampler.NewObjectSample;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import fr.digitas.flowearth.text.styles.styleManager;
	import fr.oz.core.datas.ProfilList;
	import fr.oz.core.datas.ProfilListDatas;
	import fr.oz.core.utils.Utils;
	import fr.oz.mobile.common.PictoAlarmClock_FC;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class AlarmClockPreview extends Sprite
	{
		private var tfHour:TextField;
		private var tfMinut:TextField;
		
		private var picto:PictoAlarmClock_FC;
		
		public function AlarmClockPreview() 
		{
			picto = new PictoAlarmClock_FC();
			tfHour = new TextField();
			styleManager.applyFormat(tfHour, ".home_alarm_preview_hour");
			tfHour.autoSize = TextFieldAutoSize.RIGHT;
			
			tfMinut = new TextField();
			styleManager.applyFormat(tfMinut, ".home_alarm_preview_minut");
			
			addChild(tfHour);
			addChild(tfMinut);
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function update():void
		{
			var profiles:Vector.<ProfilListDatas> = ProfilList.getActiveProfiles();
			var len:int = profiles.length;
			var date:Date = new Date();
			var hour:int = 25;
			var minut:int = 61;
			
			var day:Number = date.getDay();
			if (profiles.length != 0) {
				for (var i:int = 0; i < len; i++) 
				{
					var week:String = profiles[i].dayInWeek;
					var hash_week:Array = week.split("|");
					if (hash_week[day] == 1) {
						if (int(profiles[i].time_hour) < hour) {
							hour = int(profiles[i].time_hour);
							minut = int(profiles[i].time_minut);
						}else if (int(profiles[i].time_hour) == hour) {
							if(int(profiles[i].time_minut) < minut)	minut = int(profiles[i].time_minut);
						}
						else if (hash_week[day + 1] == 1) {
							
						}
					}
				}
				
				if(hour != 25){
					tfHour.text = Utils.addZero(""+hour);
					tfMinut.text = Utils.addZero(""+minut);
				
					tfHour.x = -tfHour.width + 2;
					tfMinut.x = -2;
					addChild(picto);
					picto.x = tfHour.x - picto.width * .5 - 4;
					picto.y = tfHour.height * .75;
					
					graphics.beginFill(0xff0000, 0);
					graphics.drawRect(picto.x - picto.width*.5, 0, picto.width + tfHour.width + tfMinut.width, picto.y + picto.height);
					graphics.endFill();
					
					this.mouseChildren = false;
				}
			}
		}
		
	}

}